package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsWorkCenter;
import net.realsee.domain.wcs.data.bo.WcsWorkCenterBo;
import net.realsee.domain.wcs.data.vo.WcsWorkCenterVo;
import net.realsee.domain.wcs.warehouse.WcsWarehouse;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsWorkCenterMapper;
import net.realsee.service.wcs.data.IWcsWorkCenterService;
import net.realsee.service.wcs.warehouse.IWcsWarehouseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工作中心/工位基础信息Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsWorkCenterServiceImpl extends ServiceImpl<WcsWorkCenterMapper, WcsWorkCenter> implements IWcsWorkCenterService {

    private final WcsWorkCenterMapper baseMapper;

    private final IWcsWarehouseService iWcsWarehouseService;

    /**
     * 查询工作中心/工位基础信息
     */
    @Override
    public WcsWorkCenterVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工作中心/工位基础信息列表
     */
    @Override
    public TableDataInfo<WcsWorkCenterVo> selectVoPage(WcsWorkCenterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWorkCenter> lqw = buildVoQueryWrapper(bo);
        Page<WcsWorkCenterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工作中心/工位基础信息列表
     */
    @Override
    public List<WcsWorkCenterVo> selectVoList(WcsWorkCenterBo bo) {
        LambdaQueryWrapper<WcsWorkCenter> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWorkCenter> buildVoQueryWrapper(WcsWorkCenterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWorkCenter> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsWorkCenter::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCenter()), WcsWorkCenter::getWorkCenter, bo.getWorkCenter());
        lqw.eq(StringUtils.isNotBlank(bo.getFacility()), WcsWorkCenter::getFacility, bo.getFacility());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsWorkCenter::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCenterType()), WcsWorkCenter::getWorkCenterType, bo.getWorkCenterType());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptWarehouse()), WcsWorkCenter::getReceiptWarehouse, bo.getReceiptWarehouse());
        lqw.eq(StringUtils.isNotBlank(bo.getPutawayWarehouse()), WcsWorkCenter::getPutawayWarehouse, bo.getPutawayWarehouse());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialWarehouse()), WcsWorkCenter::getMaterialWarehouse, bo.getMaterialWarehouse());
        lqw.eq(StringUtils.isNotBlank(bo.getCostCenter()), WcsWorkCenter::getCostCenter, bo.getCostCenter());
        lqw.eq(StringUtils.isNotBlank(bo.getParWorkCenter()), WcsWorkCenter::getParWorkCenter, bo.getParWorkCenter());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsWorkCenter::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsWorkCenter::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsWorkCenter::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsWorkCenter::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsWorkCenter::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsWorkCenter::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsWorkCenter::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsWorkCenter::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsWorkCenter::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsWorkCenter::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsWorkCenter::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsWorkCenter::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsWorkCenter::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增工作中心/工位基础信息
     */
    @Override
    public Boolean insertByBo(WcsWorkCenterBo bo) {
        WcsWorkCenter add = BeanUtil.toBean(bo, WcsWorkCenter.class);
        return this.save(add);
    }

    /**
     * 修改工作中心/工位基础信息
     */
    @Override
    public Boolean updateByBo(WcsWorkCenterBo bo) {
        WcsWorkCenter update = BeanUtil.toBean(bo, WcsWorkCenter.class);
        return this.updateById(update);
    }

    /**
     * 批量删除工作中心/工位基础信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    /**
     * 获取可选工位WorkStation
     */
    @Override
    public List<WcsWorkCenterVo> getOptionalWorkStations() {
        return baseMapper.getOptionalWorkStations();
    }

    /**
     * 查询工作中心管理列表
     */
    @Override
    public List<String> showWarehouse() {
        List<String> warehouses = new ArrayList<>();
        //查询库存
        List<WcsWarehouse> list = this.iWcsWarehouseService.lambdaQuery().groupBy(WcsWarehouse::getWarehouse)
            .isNotNull(WcsWarehouse::getWarehouse).list();

        String warehouseName = null;
        for (WcsWarehouse wcsWarehouse : list) {
            //拼接仓库编号和仓库名
            warehouseName = wcsWarehouse.getWarehouse() + "-" + wcsWarehouse.getName();
            warehouses.add(warehouseName);
        }

        return warehouses;
    }
}
